package com.whfc.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2020-12-08 19:28
 */
public class CrcUtil {

    private static Logger logger = LoggerFactory.getLogger(CrcUtil.class);

    /**
     * crc16校验
     *
     * @param bytes
     * @return
     */
    public static int crc16(byte[] bytes) {
        int crc_reg = 0xFFFF;
        int check;
        for (int i = 0; i < bytes.length; i++) {
            crc_reg = (crc_reg >> 8) ^ bytes[i];
            for (int j = 0; j < 8; j++) {
                check = crc_reg & 0x0001;
                crc_reg >>= 1;
                if (check == 0x0001) {
                    crc_reg ^= 0xA001;
                }
            }
        }
        return crc_reg;
    }

    public static void main(String[] agrs) {
        String data = "QN=20160801085857223;ST=32;CN=1062;PW=100000;MN=010000A8900016F000169DC0;Flag=5;CP=&&RtdInterval=30&&";
        int crc = crc16(data.getBytes());
        System.out.println(crc);
        System.out.println(ByteUtil.toHexString(crc));
    }
}
